package com.jl.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jl.commons.result.PageDataInfo;
import com.jl.commons.utils.CommonUtils;
import com.jl.commons.utils.TimeUtils;
import com.jl.mapper.FaultInfoMapper;
import com.jl.mapper.InspectionTaskMapper;
import com.jl.mapper.UserMapper;
import com.jl.mapper.UserRoleMapper;
import com.jl.model.FaultInfo;
import com.jl.model.User;
import com.jl.model.UserRole;
import com.jl.model.in.LoginUser;
import com.jl.model.in.UserGrade;
import com.jl.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用信息户表 服务实现类
 * </p>
 *
 * @author JiangJian
 * @since 2019-03-06
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    private UserRoleMapper userRoleMapper;
    @Autowired
    private FaultInfoMapper faultInfoMapper;
    @Autowired
    private InspectionTaskMapper inspectionTaskMapper;

    @Override
    public void selectUserDataGrid(PageDataInfo<Map<String, Object>> pageDataInfo, QueryWrapper<User> qw) {
        Page<Map<String, Object>> page = new Page<>();
        page.setSize(pageDataInfo.getSize());
        page.setCurrent(pageDataInfo.getCurrent());
        List<Map<String, Object>> mapList = baseMapper.selectDataGrid(page, qw);
        pageDataInfo.setTotal(page.getTotal());
        pageDataInfo.setRecords(mapList);
    }

    @Override
    public void getAllUserGrade(PageDataInfo<UserGrade> pageDataInfo,String month,Long userId) {
            //所有用户作为主维修人员的次数
            List<UserGrade> userGrades = faultInfoMapper.selectAllUserMasterSolverCount(month,userId);

            //所有参与维护设备的用户的维护次数
            List<Map<String,Long>> inspectFinishList =inspectionTaskMapper.selectInspectFinishCount(month,userId);
            Map<Long,Integer> inspectFinishCountMap = CommonUtils.getFinishInspectionCount(inspectFinishList);
            //如果用户id为null则获取所有的用户在本月确认维护的数量
            if(userId == null){

                //本月所有参数与辅助维修的人员的id
                List<String> assistSolverIds= faultInfoMapper.selectAllUserAssistSolverCount(month);
                Map<Long,Integer> assistSolverCountMap = CommonUtils.getStringListIdCount(assistSolverIds);
                //本月所有的确人维护数量
                List<Map<String,Long>> inspectConfirmCountList = inspectionTaskMapper.selectInspectConfirmCount(month);
                Map<Long,Integer> inspectConfirmCountMap = CommonUtils.getConfirmInspectionCount(inspectConfirmCountList);
                for(UserGrade userGrade : userGrades){
                    Long tempId = userGrade.getUserId();
                    //设置协助维修数量
                    userGrade.setAssistFaultCount(assistSolverCountMap.get(tempId) == null? 0 : assistSolverCountMap.get(tempId));
                    //设置维护设备数量
                    userGrade.setFinishTaskCount(inspectFinishCountMap.get(tempId) == null? 0 : inspectFinishCountMap.get(tempId));
                    //设置确认维护数量
                    userGrade.setConfirmTaskCount(inspectConfirmCountMap.get(tempId) == null? 0 : inspectConfirmCountMap.get(tempId));
                }
                pageDataInfo.setRecords(userGrades);
                pageDataInfo.setTotal(userGrades.size());
            }else{
                //本月某人参与辅助维修的次数
                Integer assistFaultCount = faultInfoMapper.selectAssistSolverCountByUserId(month,userId);
                //本月某人参与确认维护信息的次数
                Integer confirmTaskCount = inspectionTaskMapper.selectInspectConfirmCountByUserId(month,userId);
                //表示只是查询某个具体用户的信息
                UserGrade userGrade = userGrades.get(0);
                Long tempId = userGrade.getUserId();
                //设置协助维修数量
                userGrade.setAssistFaultCount(assistFaultCount);
                //设置维护设备数量
                userGrade.setFinishTaskCount(inspectFinishCountMap.get(tempId) == null? 0 : inspectFinishCountMap.get(tempId));
                //设置确认维护数量
                userGrade.setConfirmTaskCount(confirmTaskCount);
                pageDataInfo.setRecords(userGrades);
                pageDataInfo.setTotal(1);
            }
    }

    @Override
    public void getUserGradeById(PageDataInfo<Map<String, Object>> pageDataInfo, Long userId) {
        /*
            1、获取所有的故障维修记录(主维修1分，辅助维修0.5分)
            2、获取所有的维护记录(维护人员1分，班长和主管0.5分)
         */


    }

    /**
     * 该用户名是否已经被使用
     *
     * @param loginName 用户名
     * @return
     */
    @Override
    public boolean isLoginNameUsed(String loginName) {
        return baseMapper.selectLoginNameCount(loginName) > 0;
    }

    @Override
    public boolean isLoginNameCanUse(String loginName, Long id) {
        LoginUser loginUser = baseMapper.selectLoginUserByLoginName(loginName);
        if (loginUser == null) {
            //如果找不到数据则表示该数据没有被使用
            return true;
        } else {
            if (loginUser.getId().equals(id)) {
                //如果这个数据就是他本身则可以使用
                return true;
            }
        }
        return false;
    }

    @Override
    public boolean saveUser(User user, Long roleId) {
        boolean saveUserBoolean = baseMapper.insert(user) > 0;
        boolean updateRoleBoolean = true;
        if (saveUserBoolean) {
            UserRole userRole = new UserRole();
            userRole.setStatus(1);
            userRole.setUserId(user.getId());
            userRole.setRoleId(roleId != null ? roleId : user.getRoleId());
            updateRoleBoolean = userRoleMapper.insert(userRole) > 0;
        }
        return saveUserBoolean && updateRoleBoolean;

    }

    @Override
    public boolean updateUserById(User user, Long roleId) {
        if (baseMapper.updateById(user) > 0) {
            UserRole userRole = new UserRole();

            userRole.setRoleId(roleId != null ? roleId : user.getRoleId());
            QueryWrapper<UserRole> queryWrapper = new QueryWrapper<UserRole>();
            queryWrapper.eq("user_id",user.getId());
            return userRoleMapper.update(userRole,queryWrapper) > 0;
        }

        return false;
    }


    @Override
    public boolean deleteUserByIds(List<Long> ids) {
        QueryWrapper<UserRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("user_id",ids);
        //先删除user-role中的数据
        userRoleMapper.delete(queryWrapper);
        return baseMapper.deleteBatchIds(ids)>0;
    }

    @Override
    public User selectUserByAccount(String username) {
        List<User> list = baseMapper.selectUserByAccount(username);
        if (null != list && !list.isEmpty()) {
            return list.get(0);
        } else {
            return null;
        }
    }



}
